package com.qidian.study.dao.impl;

import com.qidian.study.servlets.BaseServlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
public class BaseDAO {
    // 连接数据库需要的常量
    private static String DRIVER_CLASS="com.mysql.jdbc.Driver";
    private static String URL = "jdbc:mysql://127.0.0.1:3306/crm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
    private static String USERNAME = "root";
    private static String PASSWORD = "root123";
    static{
        InputStream in = BaseDAO.class.getClassLoader().getResourceAsStream("db.properties");
        Properties ps = new Properties();
        try {
            ps.load(in);
            DRIVER_CLASS = ps.getProperty("jdbc.driver");
            URL = ps.getProperty("jdbc.url");
            USERNAME = ps.getProperty("jdbc.username");
            PASSWORD = ps.getProperty("jdbc.password");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    // 得到一个有效的链接
    public Connection getCon(){
        Connection con = null;
        try{
            // 加载驱动
            Class.forName(DRIVER_CLASS);
            // 创建连接
            con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return con;
    }
    // 关闭相关的资源
    public void closeAll(ResultSet rs, Statement st, Connection con){
        try{
            if(rs !=null){
                rs.close();
            }
            if(st !=null ){
                st.close();
            }
            if(con !=null ){
                con.close();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }


    // 执行增删改的操作
    // insert into admin(admin_id,admin_name) values(?,?);   kakaxi,123456
    public int executeUpdate(String sql,Object ... params){
        int result = 0;
        Connection con = null;
        PreparedStatement pst = null;
        try {
            // 得到连接
            con = getCon();
            // 创建Statement
            pst = con.prepareStatement(sql);
            // 设置参数
            for (int i = 0;i < params.length;i++){
                if(params[i] == null){
                    pst.setString(i + 1, null);
                }else {
                    pst.setString(i + 1, params[i].toString());
                }
            }
            // 执行sql语句
            result = pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(null,pst,con);
        }
        return result;
    };

    // 执行增删改的sql语句  参数就是要执行的sql
    public int executeUpdateNoParam(String sql){
        int result = 0;
        Connection con = null;
        Statement st = null;
        try {
            // 得到连接
            con = getCon();
            // 创建Statement
            st = con.createStatement();
            // 执行sql语句
            result = st.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(null,st,con);
        }
        return result;
    }
}
